package org.codeart;

import org.codeart.tank.model.Bullet;
import org.codeart.tank.model.Tank;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

public class TankFrame extends Frame {

    private final Tank tank = new Tank(50, 50, Dir.DOWN, this);
    
    public final List<Bullet> bullets = new ArrayList<>();

    private Image offScreenImage;

    public static final int GAME_WIDTH = 800;

    public static final int GAME_HEIGHT = 600;

    private class IListener extends KeyAdapter {

        boolean bL;

        boolean bU;

        boolean bR;

        boolean bD;

        @Override
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            switch (key) {
                case KeyEvent.VK_LEFT:
                    bL = true;
                    break;
                case KeyEvent.VK_UP:
                    bU = true;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = true;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = true;
                    break;
                default:
                    break;
            }
            setDir();
        }

        @Override
        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();
            switch (key) {
                case KeyEvent.VK_LEFT:
                    bL = false;
                    break;
                case KeyEvent.VK_UP:
                    bU = false;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = false;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = false;
                    break;
                case KeyEvent.VK_SPACE:
                    tank.fire();
                    break;
                default:
                    break;
            }
            setDir();
        }

        private void setDir() {
            if (!bL && !bR && !bU && !bD) {
                tank.setMoving(false);
                return;
            }
            tank.setMoving(true);
            if (bL) {
                tank.setDir(Dir.LEFT);
            }
            if (bU) {
                tank.setDir(Dir.UP);
            }
            if (bR) {
                tank.setDir(Dir.RIGHT);
            }
            if (bD) {
                tank.setDir(Dir.DOWN);
            }
        }
    }

    public TankFrame() throws HeadlessException {
        setVisible(true);
        setSize(GAME_WIDTH, GAME_HEIGHT);
        setResizable(false);
        setTitle("War of Tank");
        setLocationRelativeTo(null);
        addKeyListener(new IListener());
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    @Override
    public void paint(Graphics g) {
        // 显示子弹的数量
        Color c = g.getColor();
        g.setColor(Color.WHITE);
        g.drawString("Number of bullets: " + bullets.size(), 10, 60);
        g.setColor(c);
        
        tank.paint(g);
        for (int i = 0; i < bullets.size(); i++) {
            bullets.get(i).paint(g);
        }
    }

    @Override
    public void update(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
        }
        Graphics gOffScreen = offScreenImage.getGraphics();
        Color c = gOffScreen.getColor();
        gOffScreen.setColor(Color.BLACK);
        gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
        gOffScreen.setColor(c);
        paint(gOffScreen);
        g.drawImage(offScreenImage, 0, 0, null);
    }

}
